// pages/sign/sign.js
var api = require("../../utils/api.js");
var util = require("../../utils/util.js");

const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    buDay: 0,
    continuous: 0,
    todayIndex: 0,
    todaySign: 2,
    cardNum: -1,
    signMsg: null,
    signStep: 0,
    showBuView: false,
    showSignView: false,
    sign: {},
    buData:{},
  },

  intercept: function (e) {

  },


  //处理数据
  getData: function(loading = false) {
    if(loading){
      wx.showLoading();
    }
    api.signDatas(res => {
      wx.hideLoading();
      var buDay = 0;
      var continuous = res.count;
      var todaySign = 2;
      var todayIndex = 0;
      var cardNum = res.today;
      for (var i = 0; i < res.data.length; i++) {
        var info = res.data[i];
        info.dateDay = info.date.substring(info.date.length - 2); //日期获取
        info.bu = 2; //是否补签 1补签 2不可补签
        if (info.today == 1) {
          todayIndex = i;
          todaySign = info.sign; //今天签到信息
          var info1 = res.data[i - 1]; //前一天
          var info2 = res.data[i - 2]; //前2天
          if (info1 != null) {
            info1.bu = info1.sign != 1 ? 1 : 2; //是否需补签
            if (info1.sign != 1) {
              buDay++; //补签日是否增加
            }
          }
          if (info2 != null) {
            info2.bu = info2.sign != 1 ? 1 : 2;
            if (info2.sign != 1) {
              buDay++;
            }
          }
        }
      }
      this.setData({
        buDay: buDay,
        continuous: continuous,
        todaySign: todaySign,
        sign: res,
        todayIndex: todayIndex,
        cardNum:cardNum,
      });
    },()=>{
      wx.hideLoading();
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getData(true);
  },

//点击签到规则
  clickSignRule: function(e) {
    util.toSignRule();
  },
//点击签到
  clickSign: function(e) {
    wx.showLoading({});
    api.sign(res => {
      wx.hideLoading();
      this.setData({
        signMsg: res.message,
        signStep: res.sign_step,
        cardNum: res.card_name,
      });
      this.setData({
        showSignView: true,
      });
      this.getData();
    },()=>{
      wx.hideLoading();
    });

  },
//隐藏签到页面
  hideSignView: function(e) {
    this.setData({
      showSignView: false,
    });
  },
//点击补签
  clickBuSign: function(e) {
    console.log(123);
    this.setData({
      showBuView: true,
      buData: e.currentTarget.dataset.day,
    });
  },
//隐藏补签页面
  hideBuView: function(e) {
    this.setData({
      showBuView: false,
    });
  },
//补签
  buSign: function(e) {
    this.hideBuView();
    wx.showLoading();
    api.repairSign(this.data.buData.date,res=>{
      wx.hideLoading();
      wx.showToast({
        title: res.message,
      });
      this.setData({
        buData:{},
      });
      this.getData();
    },err=>{
      wx.hideLoading();
      wx.showToast({
        title: err,
        icon:'none',
      })
    });
  },

  clickMyCard: function(e) {
    util.toMyCard();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return app.shareSeekHelp();
  }
})